package com.network.retrofit_rx.Api;

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * @author QiQi
 * @ClassName: MyResponseBodyConverterFactory
 * @date 2018/6/11
 * @copyrights 浙江维融电子有限公司
 */
public class MyResponseBodyConverterFactory extends Converter.Factory {

    public static MyResponseBodyConverterFactory create() {
        return new MyResponseBodyConverterFactory();
    }


    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        if (String.class.equals(type)) {
            return new MyResponseBodyConverter();
        }
        return super.responseBodyConverter(type, annotations, retrofit);
    }

    private static final String TAG = "MyResponseBodyConverter";


    private static class MyResponseBodyConverter implements Converter<ResponseBody, String> {
        @Override
        public String convert(@NonNull ResponseBody value) throws IOException {
            //modify shibo.zheng 19/06/29 start
            String response = value.string();
            JSONObject jsonObject = JSONObject.parseObject(response);
            BaseResultEntity entity = JSONObject.parseObject(response, BaseResultEntity.class);
            Log.d(TAG, "convert: entity.getData() = " + entity.getData());
            if (TextUtils.isEmpty(entity.getData())) {
                if (entity.isSuccess()) {
                    return jsonObject.getString("message");
                }
                throw new RuntimeException(jsonObject.getString("message"));
            } else {
                Log.d(TAG, "convert: entity.isSuccess() = " + entity.isSuccess());
                if (entity.isSuccess()) {
                    Log.d(TAG, "convert:    2 " + entity.getData());
                    return entity.getData();
                } else {
                    throw new RuntimeException(entity.getDescription());
                }
            }
            //modify shibo.zheng 19/06/29 end
            //del shibo.zheng 19/07/01 start
            /*JSONObject jsonObject = JSONObject.parseObject(value.string());
            String title = jsonObject.getString("title");
            if (TextUtils.isEmpty(title)) {
                if ("200".equals(jsonObject.getString("code"))) {
                    Log.d(TAG, "convert: " + jsonObject.getString("result"));
                    return jsonObject.getString("result");
                } else {
                    throw new RuntimeException(jsonObject.getString("msg"));
                }
            } else {
                String result = jsonObject.getString("result");
                BaseResultEntity entity = JSON.parseObject(result, BaseResultEntity.class);
                if (entity.isSuccess()) {
                    Log.d(TAG, "convert: " + entity.getData());
                    return entity.getData();
                } else {
                    throw new RuntimeException(entity.getDescription());
                }
            }*/
            //del shibo.zheng 19/07/01 end
        }
    }
}
